import api from '@api'
const state = {
  orderID: '',
  coupon: {
    list: []
  },
  address: {
    list: [],
  },
  goods: {
    list: []
  },
  order: {
    id: '',
    address: null,
    goods: [],
    shop: null,
    coupon: {
      id: '',
      name: ''
    },
    integral: 0,
    price: null,
    useIntegral: false
  },
  area: {
    aShen: {
      id: '',
      text: ''
    },
    aShi:  {
      id: '',
      text: ''
    },
    aXian:  {
      id: '',
      text: ''
    },
    modal: false
  }
}

const getters = {
  ['getGoods'] (state) {
    return state.goods;
  },
  ['getOrder'] (state) {
    return state.order;
  },
  ['getCoupon'] (state) {
    return state.coupon;
  },
  ['getAddress'] (state) {
    return state.address;
  },
  ['getArea'] (state) {
    return state.area
  },
  ['goodsTC'] (state) {
    return state.order.goods.filter(function (item) {
      return item.goType === 1 || item.goType === 4
    })
  },
  ['goodsDP'] (state) {
    return state.order.goods.filter(function (item) {
      return item.goType === 0 || item.goType === 3
    })
  }
}

const actions = {
  async ['getOrder'] ({commit, state}, orderId) {
    if(state.orderID == orderId) {
      return
    }

  },
  async ['getDefaultAddress'] ({commit, state}) {
    let {data: {addressList, StatusCode}} = await api.get('api/v1/UserInfo/addressList.ashx', {isDefault: 1})
    if( StatusCode == 200) {
      if(addressList.length>0) {
        commit('setDefaultAddress', addressList[0])
      } else {
        commit('setDefaultAddress', null)
      }
    }
  },
  async ['getOrderGoods'] ({commit}, orderId) {
    let {data: {orderDetail, storeInfo, StatusCode}} = await api.get('api/v2/Order/tempOrderDetail.ashx', {orderSn: orderId});
    if(StatusCode == '200') {
      commit('setOrderGoods', orderDetail);
      commit('setOrderShop', storeInfo[0])
    }
  },
  async ['getAddress'] ({commit, state},) {
    let {data: {addressList, StatusCode}} = await api.get('api/v1/UserInfo/addressList.ashx')
    if( StatusCode == 200) {
        commit('setAddress', addressList)
    }
  },
  async ['getPrice'] ({commit}, orderId) {
    let {data} = await api.get('api/v1/Order/tempOrderMoney.ashx', {orderSn: orderId});
    if(data.StatusCode == '200') {
      commit('setPrice', data);
    }
  },
  async ['getCoupon'] ({commit}, orderId) {
    let {data: {integral, couponList, StatusCode}} = await api.get('api/v1/Order/orderCouponList.ashx', {orderSn: orderId});
    if(StatusCode == '200') {
      commit('setIntegral', parseFloat(integral));
      commit('setCouponList', couponList);
    }
  },
  async ['changePrice'] ({commit, state}) {
    let orderSn = state.order.id;
    let couponSn = state.order.coupon.id;
    let integral = state.order.useIntegral ? state.order.integral*100 : 0
    let pickUpSn = '';
    let AddressId = state.order.address? state.order.address.id : '';
    let shoppingGoods = state.order.goods;
    let {data} = await api.post('api/v1/Order/couponChoose.ashx', {
      orderSn,
      couponSn,
      integral,
      pickUpSn,
      AddressId,
      shoppingGoods
    });
    if(data.StatusCode == '200') {
      commit('setPrice', data);
    }
  },
  ['setArea'] ({commit}, {type, id, text}) {
    commit('setArea', {type, id, text})
  },
  ['showModal'] ({commit}, isShow) {
    commit('setModal', isShow)
  },
  ['setOrderId'] ({commit}, orderId) {
    commit('setOrderId', orderId)
  },
  ['chooseCoupon'] ({commit}, {id, name}) {
    commit('setCoupon', {id,name})
  },
  ['clearDefaultAddress'] ({commit}) {
    commit('clearDefaultAddress');
  },
  ['useIntegral'] ({commit, dispatch}, isUser) {
    commit('setUseIntegral' ,isUser);
    dispatch('changePrice');
  }
}
const mutations = {
  ['setDefaultAddress'] (state, address) {
    state.order.address = address
  },
  ['clearDefaultAddress'] (state) {
    state.order.address = null
  },
  ['setAddress'] (state, address) {
    state.address.list = address
  },
  ['setArea'] (state, {type, id, text}) {
    state.area[type] = {
      id,
      text
    }
  },
  ['setModal'] (state, isShow) {
    state.area.modal = isShow;
  },
  ['setOrderGoods'] (state, goodsList) {
    state.order.goods = goodsList
  },
  ['setOrderShop'] (state, shop) {
    state.order.shop = shop
  },
  ['setOrderId'] (state, orderId) {
    state.order.id = orderId
  },
  ['setPrice'] (state, price) {
    state.order.price = price
  },
  ['setIntegral'] (state, integral){
    state.order.integral = integral;
  },
  ['setCouponList'] (state, couponList) {
    state.coupon.list = couponList
  },
  ['setCoupon'] (state, {id, name}) {
    state.order.coupon.id = id;
    state.order.coupon.name = name;
  },
  ['setUseIntegral'] (state, isUse) {
    state.order.useIntegral = isUse
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

